﻿

namespace KinonekoSoftware.Studio.Models.Modules
{
    public class NumericElementPresenter : ElementPresenter<Element_Numeric>
    {

        protected override void OnInitialize(Element_Numeric _)
        {
            RaiseUpdated(nameof(Color));
            RaiseUpdated(nameof(Value));
            RaiseUpdated(nameof(Suffix));
        }
        
        /// <summary>
        /// 获得当前内容块的标题。
        /// </summary>
        /// <returns>返回当前内容块的标题。</returns>
        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_Block_Numeric : Title;
        public override bool IsMultipleVariants() => false;
        public override IEnumerable<Variant> GetVariants(string documentID) => null;
        public override Variant GetVariant(string documentID)
        {
            return new IntegerVariant
            {
                Id       = documentID,
                Metadata = Metadata,
                Value    = Value,
            };
        }
        
        public override void DataSync(Variant variant)
        {
            if (variant is StringVariant sv && int.TryParse(sv.Value, out var number))
            {
                Value = number;
            }
        }
        
        public NumericGroupPresenterBase Parent         { get; internal set; }
        public Action<string, int>       OnValueChanged { get; internal set; }

        

        public string Metadata
        {
            get => AssociatedObject?.Metadata;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }
                
                RaiseMetadataChanged(AssociatedObject.Metadata, value);
                AssociatedObject.Metadata = value;
            }
        }


        public int Minimum
        {

            get
            {
                if (AssociatedObject is null)
                {
                    return 0;
                }

                if (Parent is not null)
                {
                    return Parent.Minimum;
                }

                return AssociatedObject.Minimum;
            }
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Minimum = value;
                RaiseUpdated();
            }
        }

        public string Color
        {
            get => AssociatedObject?.Color ?? "#007ACC";
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Color = value;
                RaiseUpdated();
            }
        }

        public int Maximum
        {
            get
            {
                if (Parent is null)
                {
                    return 10;
                }

                if (Parent is not null)
                {
                    return Parent.Maximum;
                }

                return AssociatedObject.Maximum;
            }
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Maximum = value;
                RaiseUpdated();
            }
        }

        public int Value
        {
            get => AssociatedObject?.Value ?? 0;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }
                AssociatedObject.Value = value;
                OnValueChanged?.Invoke(Id, value);
                RaiseUpdated();
                RaisePropertyUpdated(Metadata);
            }
        }
        
        public string Suffix
        {
            get => AssociatedObject?.Suffix;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Suffix = value;
                RaiseUpdated();
            }
        }
    }
}